package com.daocao.ds.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.daocao.ds.R;
import com.daocao.ds.activity.OrderDetailActivity;
import com.daocao.ds.api.model.DealListModel;
import com.daocao.ds.util.Spans;
import com.daocao.ds.util.TimeUtil;
import com.daocao.ds.view.StarNumberView;

import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class HomeOrderAdapter extends ItemAdapter<DealListModel.DatasBean, HomeOrderAdapter.DefaultHolder> {


    private Context context;

    public HomeOrderAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_home_order, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        DealListModel.DatasBean item = getItem(position);
        if (item.number.equals("0")) {
            holder.txtNumber.setText("首次");
            holder.txtNumber.setBackgroundResource(R.mipmap.img_home_number_select);
        } else {
            holder.txtNumber.setText(item.number + "次");
            holder.txtNumber.setBackgroundResource(R.mipmap.img_home_number_unselect);
        }
        //  状态： 1-可抢、2 - 锁定，不可抢，3-已抢
        holder.layoutOrderPay.setVisibility(View.GONE);
        switch (item.rush_state) {
            case "1":
                holder.btnStatus.setText("可抢单");
                holder.btnStatus.setBackgroundResource(R.mipmap.img_home_status_select);
                break;
            case "2":
                holder.btnStatus.setText("抢单中");
                holder.btnStatus.setBackgroundResource(R.mipmap.img_home_status_select);
                holder.layoutOrderPay.setVisibility(View.VISIBLE);
                holder.txtOrderTime.setText("订单剩余支付时间");
                break;
            case "3":
                holder.btnStatus.setText("已被抢单");
                holder.btnStatus.setBackgroundResource(R.mipmap.img_home_status_unselect);
                break;
        }
        holder.txtName.setText(item.company_name);
        holder.txtRealname.setText(item.real_name);

        holder.txtMoney.setText(getInfoText("金额：", item.borrow_amount + ""));
        holder.txtDate.setText(getInfoText("期限：", item.limit_days + "天"));
        holder.txtRate.setText(getInfoText("年化利率：", item.rate + "%"));
        holder.imgStar.setStar(Integer.valueOf(item.level_id));
        if (!TextUtils.isEmpty(item.created_at)) {
            holder.txtTime.setText(TimeUtil.format(new Date(Long.valueOf(item.created_at) * 1000)));
        }

        holder.txtInfoOne.setText(item.use_type);
        holder.txtInfoTwo.setText(item.month_income);
        holder.txtInfoThree.setText(item.info_certs);
        holder.txtInfoFour.setText(item.asset_certs);

        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.rush_state.equals("1")) {
//                    if (onChooseListener != null) {
//                        onChooseListener.onSuccess(item.id);
//                    }
                    Intent intent = new Intent(context, OrderDetailActivity.class);
                    intent.putExtra("id", item.id);
                    context.startActivity(intent);
                }
            }
        });
    }

    private Spanned getInfoText(String one, String two) {
        if (TextUtils.isEmpty(one)) {
            one = "";
        }
        if (TextUtils.isEmpty(two)) {
            two = "";
        }
        return Spans.with(context).font(one, context.getResources().getDimensionPixelSize(R.dimen.x24), 0xff767676).
                font(two, context.getResources().getDimensionPixelSize(R.dimen.x32), 0xff0072FF).style(Typeface.BOLD).build();
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.layout_name)
        LinearLayout layoutName;
        @BindView(R.id.btn_status)
        TextView btnStatus;
        @BindView(R.id.txt_realname)
        TextView txtRealname;
        @BindView(R.id.img_star)
        StarNumberView imgStar;
        @BindView(R.id.txt_number)
        TextView txtNumber;
        @BindView(R.id.txt_time)
        TextView txtTime;
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.txt_date)
        TextView txtDate;
        @BindView(R.id.txt_rate)
        TextView txtRate;
        @BindView(R.id.txt_info_one)
        TextView txtInfoOne;
        @BindView(R.id.txt_info_two)
        TextView txtInfoTwo;
        @BindView(R.id.txt_info_three)
        TextView txtInfoThree;
        @BindView(R.id.txt_info_four)
        TextView txtInfoFour;
        @BindView(R.id.txt_order_time)
        TextView txtOrderTime;
        @BindView(R.id.btn_pay)
        SuperButton btnPay;
        @BindView(R.id.layout_order_pay)
        FrameLayout layoutOrderPay;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onSuccess(String id);
    }
}
